/**
 * Copyright (C) 2014 xspace app Project.
 */

package cn.xspace.module.other.http.impl;

import com.xspace.android.xafcommon.network.NetConstant;
import com.xspace.android.xafcommon.network.message.AResponseMsg;
import com.xspace.android.xafcommon.network.message.SendRequestListener;

import android.content.Context;
import android.widget.Toast;
import cn.xspace.module.other.activity.RegisterAct;
import cn.xspace.module.other.http.NetControl;
import cn.xspace.module.other.http.intface.IRegister;
import cn.xspace.module.other.http.resp.RegisterResp;
import cn.xspace.module.other.util.RProxy;

/**
 * @ClassName: RegisterImpl
 * @author:mawen
 * @Date：2014-2-25
 * @version V1.0.0
 * @Description: TODO
 */
public class RegisterImpl implements SendRequestListener, IRegister {
	private NetControl netControl;
	private Context mContext;

	/**
	 * @Description: TODO
	 * @param
	 * @return
	 * @throws
	 */
	public RegisterImpl(Context context) {
		this.mContext = context;
		netControl = new NetControl(this);
	}

	/*
	 * <p>Title: register</p> <p>Description: </p>
	 * 
	 * @param mobile
	 * 
	 * @param pwd
	 * 
	 * @param vertifycode
	 * 
	 * @see
	 * cn.xspace.app.other.logic.intface.IRegister#register(java.lang.String,
	 * java.lang.String, java.lang.String)
	 */
	@Override
	public void register(String mobile, String pwd, String vertifycode) {
		netControl.sendRegisterReq(mobile, pwd, vertifycode);
	}

	/*
	 * <p>Title: requestResult</p> <p>Description: </p>
	 * 
	 * @param result
	 * 
	 * @param object
	 * 
	 * @see com.xspace.android.xafcommon.network.message.SendRequestListener#
	 * requestResult(int,
	 * com.xspace.android.xafcommon.network.message.AResponseMsg)
	 */
	@Override
	public void requestResult(int result, AResponseMsg object) {
		String errorTip = "";
		((RegisterAct) mContext).cancelDialog();
		if (object.msgType.equals(NetControl.MSGTYPE_REGISTER)) {
			if (result == NetConstant.TaskStatus.SUCCESS) {
				RegisterResp resp = (RegisterResp) object;
				if (resp.registerBean.code.equals(String
						.valueOf(RegisterResp.REGISTER_SUCCESS))) {// 注册成功
					((RegisterAct) mContext)
							.registerSuccess(resp.registerBean.desc);
					return;
				} else if (resp.registerBean.code.equals(String
						.valueOf(RegisterResp.REGISTERED))) {// 已注册
					errorTip = mContext
							.getString(RProxy.registeract_register_registered);
				} else if (resp.registerBean.code.equals(String
						.valueOf(RegisterResp.REGISTER_VERTIFYCODE_ERROR))) {// 注册验证码错误
					errorTip = mContext
							.getString(RProxy.registeract_register_vertifycode_error);
				} else {// 注册失败
					errorTip = mContext
							.getString(RProxy.registeract_register_error);
				}
			} else {
				errorTip = mContext.getString(RProxy.network_fail);
			}
			Toast.makeText(mContext, errorTip, Toast.LENGTH_LONG).show();
		}
	}

	/*
	 * <p>Title: offline</p> <p>Description: </p>
	 * 
	 * @param msgType
	 * 
	 * @see
	 * com.xspace.android.xafcommon.network.message.SendRequestListener#offline
	 * (java.lang.String)
	 */
	@Override
	public void offline(String msgType) {
		((RegisterAct) mContext).cancelDialog();
		Toast.makeText(mContext, RProxy.network_fail, Toast.LENGTH_SHORT)
				.show();
	}
}
